package org.freedoit.dao;

// Generated Oct 11, 2012 3:54:06 PM by Hibernate Tools 3.4.0.CR1

import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.freedoit.FreeDoitConfig;
import org.freedoit.om.TaskType;
import org.freedoit.util.LogUtils;

/**
 * Home object for domain model class TaskType.
 * @see org.freedoit.om.TaskType
 * @author Hibernate Tools
 */
public class TaskTypeDAO {
	private static final Logger log = LogUtils.getLogger(TaskDAO.class);
    List<TaskType> cachedTaskTypeList = null;
    public TaskTypeDAO() {
        cachedTaskTypeList = loadAll();
    }

	public List<TaskType> findAll() {
        if (cachedTaskTypeList == null) {
            return null;
        } else {
            return new LinkedList<TaskType>(cachedTaskTypeList);
        }
    }

	private List<TaskType> loadAll() {
        String strTaskTypeList = FreeDoitConfig.getText("global.task.type.list");
        List<TaskType> taskTypeList = null;
        if (strTaskTypeList != null && !"".equals(strTaskTypeList)) {
            String[] arrayTaskType = strTaskTypeList.split(",");
            if (arrayTaskType != null && arrayTaskType.length > 0) {
                taskTypeList = new LinkedList<TaskType>();
                TaskType taskType = null;
                
                for (String str : arrayTaskType) {
                    if ((taskType = parseTaskType(str)) != null) {
                        taskTypeList.add(taskType);
                    }
                }
            }
        }
        
        return taskTypeList;
	}
    
    private TaskType parseTaskType(String strTaskType) {
        String[] idAndName = strTaskType.split(":");
        TaskType taskType = null;
        if (idAndName != null && idAndName.length == 2) {
            Long lId = null;
            
            try {
                lId = Long.valueOf(idAndName[0]);
            } catch (Exception e) {
                log.warn(e);
            }
            
            String strName = idAndName[1];
            if (lId != null && strName != null && !"".equals(strName)) {
                taskType = new TaskType(lId, strName);
            }
        }
        
        return taskType;
    }
}
